﻿using BatchQueryWebApi.Models;
using BatchQueryWebApi.Services;
using Microsoft.AspNetCore.Mvc;

namespace BatchQueryWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DeviceController : ControllerBase
    {
        private readonly IDeviceStatusService _deviceStatusService;

        public DeviceController(IDeviceStatusService deviceStatusService)
        {
            _deviceStatusService = deviceStatusService;
        }

        [HttpGet("status/batch")]
        public async Task<ActionResult<IEnumerable<DeviceStatusResult>>> GetDeviceStatusesInBatches()
        {
            // 模拟100个设备ID
            var deviceIds = GenerateDeviceIds(100);

            const int batchSize = 10; // 每批处理10个设备

            var allResults = new List<DeviceStatusResult>();

            try
            {
                // 分批次处理
                for (int i = 0; i < deviceIds.Count; i += batchSize)
                {
                    var currentBatch = deviceIds.GetRange(i, Math.Min(batchSize, deviceIds.Count - i));

                    Console.WriteLine($"开始处理批次 {i / batchSize + 1}, 设备数量: {currentBatch.Count}");

                    // 并行处理当前批次
                    var batchResults = await _deviceStatusService.GetDeviceStatusesAsync(
                        currentBatch,
                        TimeSpan.FromSeconds(5) // 5秒超时
                    );

                    allResults.AddRange(batchResults);

                    Console.WriteLine($"完成处理批次 {i / batchSize + 1}");
                }

                return Ok(allResults);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"查询设备状态时发生错误: {ex.Message}");
            }
        }


        [HttpGet("status/batch1")]
        public async Task<ActionResult<IEnumerable<DeviceStatusResult>>> GetDeviceStatusesInBatches1()
        {
            // 模拟100个设备ID
            var deviceIds = GenerateDeviceIds(100);

            const int batchSize = 10; // 每批处理10个设备

            int pageCount = (int)Math.Ceiling(deviceIds.Count / (double)batchSize);

            var allResults = new List<DeviceStatusResult>();

            try
            {
                // 分批次处理
                for (int page = 0; page < pageCount; page++)
                {
                    var currentBatch = deviceIds.Skip(page * batchSize).Take(batchSize);

                    Console.WriteLine($"开始处理批次 {page + 1}, 设备数量: {currentBatch.Count()}");

                    // 并行处理当前批次
                    var batchResults = await _deviceStatusService.GetDeviceStatusesAsync(
                        currentBatch,
                        TimeSpan.FromSeconds(5) // 5秒超时
                    );

                    allResults.AddRange(batchResults);

                    Console.WriteLine($"完成处理批次 {page + 1}");
                }

                return Ok(allResults);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"查询设备状态时发生错误: {ex.Message}");
            }
        }

        // 生成模拟设备ID
        private List<string> GenerateDeviceIds(int count)
        {
            var deviceIds = new List<string>();
            for (int i = 1; i <= count; i++)
            {
                deviceIds.Add($"DEVICE-{i:D3}");
            }
            return deviceIds;
        }
    }
}
